//
//  ZYCTeaLeaveOptionalCell.m
//  ZDSC
//
//  Created by 张威 on 2021/10/20.
//

#import "ZYCTeaLeaveOptionalCell.h"

#define  BTProblemTotalLenght 50
@interface ZYCTeaLeaveOptionalCell ()<UITextViewDelegate>

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIView *line;
@property (nonatomic, strong) UILabel *numLabel;
@property (nonatomic, strong) UILabel *placeHoldLabel;
@property (nonatomic, strong) UITextView *textView;
@end

@implementation ZYCTeaLeaveOptionalCell

- (void)setTitleString:(NSString *)titleString{
    _titleString = titleString;
    _titleLabel.text = titleString;
}

- (void)setPlaceHolderString:(NSString *)placeHolderString{
    _placeHolderString = placeHolderString;
    _placeHoldLabel.text = placeHolderString;
}

- (void)setContentString:(NSString *)contentString{
    _contentString = contentString;
    _textView.text = contentString;
}
- (void)setIsHid:(BOOL)isHid{
    _isHid = isHid;
    if (isHid) {
        _numLabel.hidden = YES;
        _placeHoldLabel.hidden = YES;
        _textView.editable = NO;
        
    }else{
        _numLabel.hidden = NO;
        _placeHoldLabel.hidden = NO;
        _textView.editable = YES;
    }
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self.contentView addSubview:self.titleLabel];
        [self.contentView addSubview:self.line];
        [self.contentView addSubview:self.textView];
        [self.contentView addSubview:self.numLabel];
        [self.contentView addSubview:self.placeHoldLabel];
    
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView).offset(15.0f);
            make.top.equalTo(self.contentView).offset(20.0f);
        }];
        
        [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.contentView);
            make.top.equalTo(self.titleLabel.mas_bottom).offset(20.0f);
            make.height.mas_equalTo(1.0f);
        }];
        
        [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView).offset(15.0f);
            make.right.equalTo(self.contentView).offset(-15.0f);
            make.top.equalTo(self.line.mas_bottom).offset(15.0f);
            make.bottom.equalTo(self.contentView);
        }];
        
        [self.placeHoldLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.equalTo(self.textView);
        }];

        [self.numLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.right.equalTo(self.textView);
        }];
        
    }
    return self;
}

#pragma mark UITextViewDelegate
-(void) textViewDidEndEditing:(UITextView *)textView{
    if (textView.text.length < 1) {
        self.placeHoldLabel.hidden = NO;
    }else{
        self.placeHoldLabel.hidden = YES;
    }
    if (self.textEditEndBlock) {
        self.textEditEndBlock(self.textView.text);
    }
}

-(void) textViewDidChange:(UITextView *)textView{
    self.numLabel.text = [NSString stringWithFormat:@"%ld/%d", textView.text.length, BTProblemTotalLenght];
}

-(BOOL) textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    if (textView.text.length >= BTProblemTotalLenght && text.length > 0) {
        return NO;
    }
    return YES;
}



-(void) textViewDidBeginEditing:(UITextView *)textView
{
    self.placeHoldLabel.hidden = YES;
}

#pragma mark - getters
- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [UILabel LabelWithFont:kCustomFont(@"PingFangSC-Medium", 16.0f) titleColor:HEX_RGB(@"#333333") textAlignment:ZZTextAlignmentLeft];
    }
    return _titleLabel;
}

- (UIView *)line {
    if (!_line) {
        _line = [[UIView alloc] init];
        _line.backgroundColor = HEX_RGB(@"#FAFAFA");
    }
    return _line;
}

- (UITextView *)textView {
    if (!_textView) {
        _textView = [[UITextView alloc] init];
        _textView.delegate = self;
        _textView.font = kCustomFont(@"PingFangSC-Light", 15.0f);
        _textView.textColor = HEX_RGB(@"333333");

    }
    return _textView;
}

- (UILabel *)placeHoldLabel {
    if (!_placeHoldLabel) {
        _placeHoldLabel = [UILabel LabelWithFont:kCustomFont(@"PingFangSC-Light", 15.0f) titleColor:HEX_RGB(@"#D1D1D1") textAlignment:ZZTextAlignmentLeft];
    }
    return _placeHoldLabel;
}

- (UILabel *)numLabel {
    if (!_numLabel) {
        _numLabel = [UILabel LabelWithString:@"0/50" Font:kCustomFont(@"PingFangSC-Light", 15.0f) titleColor:HEX_RGB(@"#999999") textAlignment:ZZTextAlignmentLeft];
    }
    return _numLabel;
}

@end
